package com.yxy.service.impl;

import com.yxy.context.BaseContext;
import com.yxy.enumeration.RateEnum;
import com.yxy.exception.ParamException;
import com.yxy.mapper.RateMapper;
import com.yxy.pojo.dto.RateDTO;
import com.yxy.pojo.dto.TypeDTO;
import com.yxy.pojo.entity.Rate;
import com.yxy.pojo.vo.RateVO;
import com.yxy.service.RateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RateServiceImpl implements RateService {

    @Autowired
    private RateMapper rateMapper;

    /**
     * 用户评分
     * @param rateDTO
     */
    @Transactional
    @Override
    public void rate(RateDTO rateDTO) {
        if(rateDTO.getType() == null || rateDTO.getTypeId() == 0 || rateDTO.getRateVal() == 0){
            throw new ParamException("参数错误");
        }
        // 获取用户id
        Integer userId = BaseContext.getCurrentId();
        Rate rate = new Rate();
        BeanUtils.copyProperties(rateDTO, rate);
        rate.setUserId(userId);
        rate.setCreateTime(LocalDate.now());
        // 查询当前用户是否已经评分过
        Rate r = rateMapper.getRateByUserId(rate.getType(), rate.getTypeId(), rate.getUserId());
        // 两种情况
        if(r == null){
            // 1. 如果用户没有评分，则插入一条评分记录
            rateMapper.insertRate(rate);
        }else{
            // 2. 如果用户已经评分，则更新评分记录
            rateMapper.updateRate(rate);
        }
    }

    /**
     * 获取评分
     * @param typeDTO
     * @return
     */
    @Override
    public RateVO getRate(TypeDTO typeDTO) {
        RateVO rateVO = new RateVO();
        // 当前用户评分
        int userId = typeDTO.getUserId();
        if(userId != 0){
            Rate rate = rateMapper.getRateByUserId(typeDTO.getType(), typeDTO.getTypeId(), userId);
            if(rate == null){
                rateVO.setUserRate(0);
            }else{
                rateVO.setUserRate(rate.getRateVal() / 2);
            }
        }
        // 平均评分
        List<Integer> rateAllList = rateMapper.getRateAll(typeDTO.getType(), typeDTO.getTypeId());
        double sum = rateAllList.stream().mapToDouble(item -> item).sum();
        if(sum != 0){
            rateVO.setAvgRate(Math.round(sum / rateAllList.size() * 10) / 10.0);
        }
        // 评分人数
        rateVO.setRateCount(rateAllList.size());
        // 不同星级百分比
        List<Map<String, Object>> rateList = new ArrayList<>();
        for (int i = 5; i > 0; i--){
            Map<String, Object> map = new HashMap<>();
            map.put("rank", i);
            int finalI = i;
            map.put("percentage", Math.round((float) rateAllList.stream().filter(item -> item == RateEnum.getValueByRate(finalI)).count() / rateAllList.size() * 100) / 100.0);
            rateList.add(map);
        }
        rateVO.setRateList(rateList);
        return rateVO;
    }
}
